/*
 * Dot.fx
 *
 * Created on 18/03/2010, 09:27:34 AM
 */

package util;

/**
 * @author Alex
 */

import java.lang.Math;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

public class Dot extends CustomNode {

   public var dotType: Integer;

   // location of the dot
   public var x : Number ;
   public var y : Number ;

   // radius of the dot
   public var r: Number =
   if ( dotType == Map.MAGIC_DOT ) (224/29) else 2.5;

   public var o: Number = 1.0;

   // the dot
   var circle = Circle{
      centerX: x
      centerY: y
      opacity: bind o
      radius: r
      fill: Color.YELLOW
      visible: bind visible   // bind to Dot.visible
      } ;

   // variables for magic dot's growing/shrinking animation
   public var animationAlpha = 1.0;
   public var delta = 0.08;
   var timeline: Timeline;

   // create the animation timeline for magic dot
   public function createTimeline(): Timeline {
      Timeline {
         repeatCount: Timeline.INDEFINITE
         keyFrames: [
           KeyFrame {
              time: 15ms
              action: function() {
                 doOneTick();
               }
            }
         ]
           }
    }

   public function playTimeline() {
      if ( timeline == null )
      timeline = createTimeline();

      timeline.play();
    }

   // do the animation
   public function doOneTick () {

      if ( visible == false )
      return;

      animationAlpha -= delta;
      var x = Math.abs(animationAlpha);

      if ( x < 0){
         x = 0;
         delta = -delta;
      }else if(x > 1){
         x = 1;
         delta = -delta;
      }
      o = x;

    }

   public override function create(): Node {
          return circle;
    }
}
